package com.ophiux.cs.application.consultOrder.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.cs.application.consultOrder.dto.CsOrderRecordDetailResponse;
import com.ophiux.cs.application.consultOrder.dto.GetCSOrderRecordsRequest;
import com.ophiux.cs.application.consultOrder.dto.GetCSOrderRecordsResponse;
import com.ophiux.cs.application.consultOrder.service.IGetCSOrderRecordsAppService;
import com.ophiux.cs.application.core.ResponsePage;
import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.ResultEnum;
import com.ophiux.cs.domain.consultOrder.model.aggregate.OnlineRecordsAndDetail;
import com.ophiux.cs.domain.consultOrder.model.valueobj.OnlineRecordValueObj;
import com.ophiux.cs.domain.consultOrder.repositories.ICSOnlineRecordRepository;
import com.ophiux.utils.common.ShiftUtils;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class GetCSOrderRecordsAppService implements IGetCSOrderRecordsAppService {
	@Autowired
	ICSOnlineRecordRepository csOnlineRecordRepository;
	
	
	@Override
	public Result<ResponsePage<GetCSOrderRecordsResponse>> GetCSOrderRecords(GetCSOrderRecordsRequest dto) {
		ResponsePage<GetCSOrderRecordsResponse> respPage = new ResponsePage<GetCSOrderRecordsResponse>();
		List<GetCSOrderRecordsResponse> resp = new ArrayList<GetCSOrderRecordsResponse>();
		try {
			if(!dto.isParamValid()) {
				return Result.fail(ResultEnum.requestParamFail);
			}
			
			OnlineRecordValueObj obj = (OnlineRecordValueObj) ShiftUtils.objToNewObj(dto, OnlineRecordValueObj.class);
			List<OnlineRecordsAndDetail> record = csOnlineRecordRepository.getInfoList(obj);
			for (OnlineRecordsAndDetail onlineRecordsAndDetail : record) {
				GetCSOrderRecordsResponse res = new GetCSOrderRecordsResponse();
//				res.setAPPLY_TIME(onlineRecordsAndDetail.getAPPLY_TIME());
//				res.setBIRTH_DAY(onlineRecordsAndDetail.getBIRTH_DAY());
//				res.setCS_END_TIME(onlineRecordsAndDetail.getCS_END_TIME());
//				res.setCS_PUBLISH_WAY(onlineRecordsAndDetail.getCS_PUBLISH_WAY());
//				res.setCS_START_TIME(onlineRecordsAndDetail.getCS_START_TIME());
//				res.setCS_TYPE_ID(onlineRecordsAndDetail.getCS_TYPE_ID());
//				res.setCS_TYPE_NAME(onlineRecordsAndDetail.getCS_TYPE_NAME());
//				res.setDOCTOR_ID(onlineRecordsAndDetail.getDOCTOR_ID());
//				res.setID_NO(onlineRecordsAndDetail.getID_NO());
//				res.setIS_APPRAISE(onlineRecordsAndDetail.getIS_APPRAISE());
//				res.setORDER_TIME(onlineRecordsAndDetail.getORDER_TIME());
//				res.setPATIENT_NAME(onlineRecordsAndDetail.getPATIENT_NAME());
//				res.setPHONE(onlineRecordsAndDetail.getPHONE());
//				res.setRECORD_ID(onlineRecordsAndDetail.getRECORD_ID());
//				res.setSEX(onlineRecordsAndDetail.getSEX());
//				res.setUSER_ID(onlineRecordsAndDetail.getUSER_ID());
//				res.setDOCTOR_NAME(onlineRecordsAndDetail.getDOCTOR_NAME());
//				res.setHOSPITAL_NAME(onlineRecordsAndDetail.getHOSPITAL_NAME());
//				res.setDEPT_NAME(onlineRecordsAndDetail.getDEPT_NAME());
				
				res = (GetCSOrderRecordsResponse) ShiftUtils.objToNewObj(onlineRecordsAndDetail, GetCSOrderRecordsResponse.class);
				CsOrderRecordDetailResponse detail = new CsOrderRecordDetailResponse();
				detail = (CsOrderRecordDetailResponse) ShiftUtils.objToNewObj(onlineRecordsAndDetail.getRECORD_DESCRIPTION(), CsOrderRecordDetailResponse.class);
				res.setRECORD_DESCRIPTION(detail);
				if(detail!=null) {
					res.setRECORD_DESCRIPTION(detail);
				}
				resp.add(res);
			}
			
//			resp = ShiftUtils.objToNewObjList(record, GetCSOrderRecordsResponse.class);
			respPage.initData(resp,record.size(), obj);
			
		} catch (Exception e) {
			log.info("程序异常：",e);
			e.printStackTrace();
			return Result.fail(ResultEnum.requestException);
		}
		return Result.success(respPage);
	}
}
